package com.asurplus.api.controller;

import com.asurplus.common.redis.RedisLockUtils;
import com.asurplus.common.sms.SmsUtils;
import com.asurplus.common.utils.R;
import com.asurplus.system.service.SysDictDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通用接口
 *
 * @Author admin
 **/
@Api(tags = "通用接口")
@RestController
@RequestMapping("api/common")
public class CommonController {

    @Autowired
    private SmsUtils smsUtils;
    @Autowired
    private RedisLockUtils redisLockUtils;
    @Autowired
    private SysDictDetailService sysDictDetailService;

    @ApiOperation(value = "发送短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true)
    })
    @PostMapping("sendSmsCode")
    public R sendSmsCode(String phone) {
        // 幂等性判断
        if (!redisLockUtils.setLock("sendSmsCode:" + phone)) {
            return R.no("处理中，请勿重复提交");
        }
        return smsUtils.sendSmsCode(phone);
    }

    @ApiOperation(value = "获取字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictCode", value = "字典编码", required = true)
    })
    @PostMapping("listDictData")
    public R listDictData(String dictCode) {
        if (StringUtils.isBlank(dictCode)) {
            return R.no("请输入必要参数");
        }
        return R.ok(sysDictDetailService.listSysDictDetailByDictCode(dictCode));
    }
}
